package ha.web.servlet.game;

import ha.web.database.UserId;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class GamePage
 */
@WebServlet("/GamePage")
public class GamePage extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GamePage() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ServletContext context = getServletContext();
  	  	RequestDispatcher dispatcher = context.getRequestDispatcher("/Login");
  	  	dispatcher.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
      Map<String,String[]> requestParm = request.getParameterMap();
      if (requestParm.containsKey("moves")){
  		PrintWriter out = response.getWriter();
  		out.print("responseeeeeeeeee");
  		return;
      }

      String userName = request.getParameter("user");
      String userPass = request.getParameter("pass");
      UserId user = new UserId(userName, userPass);
      if(user.IsUserAtDB(user)) 
    	  DrawGame(request, response, String.valueOf(user.getUserID()));
      else {
    	  ServletContext context = getServletContext();
    	  RequestDispatcher dispatcher = context.getRequestDispatcher("/Login");
    	  dispatcher.forward(request, response);
    	  
  		}
	}
	
	protected void DrawGame(HttpServletRequest request, HttpServletResponse response, String UId) throws IOException {
		PrintWriter out = response.getWriter();
		out.print("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">");
		out.print("<HTML><HEAD><TITLE>game page</TITLE>");
		out.print("<META http-equiv=Content-Type content=\"text/html\">");
		out.print("<link rel=\"stylesheet\" type=\"text/css\" href=\"gameDesign.css\" />");
		out.print("</HEAD><BODY><P>");
		out.print("<div align=\"center\">");
		out.print("<h2>Welcome</h2>");
		out.print("<h1>2P chinese checkers</h1>");
		out.print("<h3 id =turn_status>your turn</h3>");
		out.print("<table><tr>");
		out.print("<td><div id=\"boardplace\" align=\"center\"></div></td>");
		out.print("<td valign=\"baseline\" width=\"200\"><u>CHAT</u><br>");
		out.print("<p align=\"right\">");
		out.print("<input type=\"text\" name=\"moves\" id =\"moves\" size=\"27\"></input>");
		out.print("<input type=\"submit\" class=\"button\" value=\"send\">");
		out.print("</p>");
		out.print("<div id=\"chatplace\" align=\"center\"></div></td>");
		out.print("</tr>");
		out.print("list of moves: <input type=\"text\" name=\"moves\" id =\"moves\" size=\"80\"></input>");
		out.print("<input type=\"submit\" id=\"moves\" onclick=submit_move() class=\"button\" value=\"End turn\">");
		out.print("<br><button type=\"button\" class=\"button\" onclick=game_rules()>game rules</button>");
		out.print("<script src=\"game_window.js\" type=\"text/javascript\"></script>");
		out.print("<input hidden=\"uid\" id=\"uid\" value="+UId +"</input>");
		
		out.print("<P></P>");
		out.print("</div>");
		out.print("</BODY></HTML>");

	}
}
